package team.niit.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import team.niit.entity.NotificationEntity;
import team.niit.service.NotificationService;
import team.niit.service.impl.NotificationServiceImpl;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@ManagedBean(name = "notification")
@SessionScoped
@Data
@AllArgsConstructor
public class NotificationBean {
    private Integer id;
    private String title;
    private String major;
    private String content;

    NotificationService notificationService = new NotificationServiceImpl();

    private long allnum = notificationService.countAllNotificationNum();

    private long savednum = notificationService.countSavedNotificationNum();


    public String init() {
        FacesContext.getCurrentInstance().getViewRoot().getChildren().clear();
        return "mail_show";
    }

    public long getSavednum() {
        return savednum = notificationService.countSavedNotificationNum();
    }

    public NotificationBean() {
    }

    private List<NotificationEntity> savedList = notificationService.querySavedNotificationByPageNum(1);

    public List<NotificationEntity> getSavedList() {
        return savedList = notificationService.querySavedNotificationByPageNum(1);
    }

    private List<NotificationEntity> list = notificationService.queryNotificationByPageNum(1);

    public List<NotificationEntity> getList() {
        return list = notificationService.queryNotificationByPageNum(1);
    }

    public String doMailPost() {
        // System.out.println(content+"    "+major+"    "+title);
        NotificationEntity entity = new NotificationEntity();
        entity.setMajor(major);
        entity.setContent(content);
        entity.setTitle(title);
        entity.setSaved(0);
        entity.setTime(new Date().toString());

        notificationService.publishNotification(entity);

        return "mail_show";
    }

    public String saveNotification() {
        // System.out.println(content+"    "+major+"    "+title);

        NotificationEntity entity = new NotificationEntity();
        entity.setMajor(major);
        entity.setContent(content);
        entity.setTitle(title);
        entity.setSaved(1);
        entity.setTime(new Date().toString());

        notificationService.publishNotification(entity);

        return "mail_saved";
    }

    public String continueEdit() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String ID = request.getParameter("id");
        int id = Integer.parseInt(ID);

        NotificationEntity notificationEntity = notificationService.querySavedNotificationById(id);
        this.id = id;
        major = notificationEntity.getMajor();
        title = notificationEntity.getTitle();
        content = notificationEntity.getContent();
        return "mail_continue_edit";
    }

    public String postSavedMail() {
        NotificationEntity entity = new NotificationEntity();
        entity.setId(this.id);
        entity.setMajor(major);
        entity.setContent(content);
        entity.setTitle(title);
        entity.setSaved(0);
        entity.setTime(new Date().toString());

        notificationService.modifyNotification(entity);
        savedList = notificationService.querySavedNotificationByPageNum(1);
        return "mail_saved";
    }

    public String deleteSaved() {
        notificationService.deleteNotificationById(savedList.get(0).getId());
        savedList = notificationService.querySavedNotificationByPageNum(1);
        return "mail_saved";
    }

    public String studentsViewMail() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String ID = request.getParameter("id");
        int id = Integer.parseInt(ID);

        NotificationEntity notificationEntity = notificationService.queryNotificationById(id);
        major = notificationEntity.getMajor();
        title = notificationEntity.getTitle();
        content = notificationEntity.getContent();
        return "mail_detailed_show";
    }


    public String deleteMail() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String ID = request.getParameter("id");
        int id = Integer.parseInt(ID);

        notificationService.deleteNotificationById(id);
        list = notificationService.queryNotificationByPageNum(1);

        return "mail_show";
    }

    /**
     * 以下是关于展示通知的分页的属性和方法
     */
    private HtmlDataTable dataShow;
    private HtmlCommandButton btn1, btn2, btn3;
    private static int i = 1;

    int page = (int) notificationService.countNotificationPageNum();
    private int p1 = 1;
    private int p2 = i;
    private int p3 = page;


    public String nextPage() {
        if (i < page) {
            i++;
        } else {
            i = page;
        }
        this.getBtn2().setValue(i);
        this.getDataShow().setValue(notificationService.queryNotificationByPageNum(i));
        return "mail_show";
    }


    public String lastPage() {
        if (i > 1) {
            i--;
        } else {
            i = 1;
        }
        this.getBtn2().setValue(i);
        this.getDataShow().setValue(notificationService.queryNotificationByPageNum(i));
        return "mail_show";
    }


    public String clickBtn1() {
        this.getBtn1().setValue(1);
        i = 1;
        this.getBtn2().setValue(i);
        this.dataShow.setValue(notificationService.queryNotificationByPageNum(i));
        return "mail_show";
    }

    public String clickBtn2() {
        this.getBtn2().setValue(i);
        this.dataShow.setValue(notificationService.queryNotificationByPageNum(i));
        return "mail_show";
    }

    public String clickBtn3() {
        this.getBtn3().setValue(page);
        i = page;
        this.getBtn2().setValue(i);
        this.dataShow.setValue(notificationService.queryNotificationByPageNum(i));
        return "mail_show";
    }

    /**
     * 以下是关于草稿查询的参数和方法
     */

    private HtmlDataTable saveddataShow;
    private HtmlCommandButton sbtn1, sbtn2, sbtn3;
    private static int j = 1;


    int spage = (int) notificationService.countSavedNotificationPageNum();
    private int sp1 = 1;
    private int sp2 = j;
    private int sp3 = spage;


    public String nextSavedPage() {
        if (j < spage) {
            j++;
        } else {
            j = spage;
        }
        this.getSbtn2().setValue(j);
        this.getSaveddataShow().setValue(notificationService.querySavedNotificationByPageNum(j));
        return "mail_saved";
    }


    public String lastSavedPage() {
        if (j > 1) {
            j--;
        } else {
            j = 1;
        }
        this.getSbtn2().setValue(j);
        this.getSaveddataShow().setValue(notificationService.querySavedNotificationByPageNum(j));
        return "mail_saved";
    }


    public String clickSbtn1() {
        this.getSbtn1().setValue(1);
        j = 1;
        this.getSbtn2().setValue(j);
        this.saveddataShow.setValue(notificationService.querySavedNotificationByPageNum(j));
        return "mail_saved";
    }

    public String clickSbtn2() {
        this.getSbtn2().setValue(j);
        this.saveddataShow.setValue(notificationService.querySavedNotificationByPageNum(j));
        return "mail_saved";
    }

    public String clickSbtn3() {
        this.getSbtn3().setValue(spage);
        j = spage;
        this.getSbtn2().setValue(j);
        this.saveddataShow.setValue(notificationService.querySavedNotificationByPageNum(j));
        return "mail_saved";
    }
}
